package com.miao.service.impl;

import com.miao.entity.KnowledgeDocument;
import com.miao.repository.EsDocumentRepository;
import com.miao.entity.EsDocumentEntity;
import com.miao.entity.MyDateTest;
import com.miao.repository.KnowledgeDocumentRepository;
import com.miao.sendList.TestSend;
import com.miao.service.TestService;
import com.miao.test.UserInfo;
import com.miao.util.JSONUtils;
import com.miao.util.RabbitMQQueueConstant;
import com.miao.util.Result;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author <a href="https://gitee.com/a-tom-is-cry">Xing</a>
 * @Date 2025/9/10 22:43
 * @Description
 */
@Service
@Log4j2
public class TestServiceImpl implements TestService {

    @Resource
    private TestSend send;

    @Resource
    private EsDocumentRepository repository;

    @Resource
    private KnowledgeDocumentRepository knowledgeDocumentRepository;

    private ThreadPoolExecutor executor = new ThreadPoolExecutor(10,10,10, TimeUnit.SECONDS,new ArrayBlockingQueue<>(100));

    @RabbitListener(queues = {RabbitMQQueueConstant.TEST_QUEUE},concurrency = "1-2")
    public void test(UserInfo userInfo) throws InterruptedException {
        Thread.sleep(4000);
        log.info("消息消费：{}",userInfo);
    }

    @Override
    public void testSend(String test) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(test);
        userInfo.setAge("12");
        int i = 10;
        while (i>0){
            send.testQueueSend(JSONUtils.objToJson(userInfo));
            log.info("发送成功");
            i--;
        }
    }

    @Override
    public void delayedTestSend(String msg) {
        for (int i=0;i<5;i++){
            ///用线程池,将任务后台化，减少卡顿时间。适用于大批量费时任务
            ///  可以应用在excel导入保存或者修改中
            executor.execute(()->{
                MyDateTest myDateTest = new MyDateTest();
                myDateTest.setMessage(msg);
                myDateTest.setSendDate(new Date());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                send.delayedQueueSend(JSONUtils.objToJson(myDateTest));
                log.info("发送成功");
            });
        }
    }

    @Override
    public void Save(EsDocumentEntity entity) {
        repository.save(entity);
    }

    @Override
    public List<EsDocumentEntity> list(String key) {
        List<EsDocumentEntity> esDocumentEntitiesByKey = repository.findEsDocumentEntitiesByKey(key);
        return esDocumentEntitiesByKey;
    }

    @Override
    public Result<Object> knowList() {
        List<KnowledgeDocument> list = knowledgeDocumentRepository.list();
        return new Result<>().success(list);
//        return new Result<>();
    }

    @Override
    public Result<Object> save(KnowledgeDocument document) {
        knowledgeDocumentRepository.save(document);
        return new Result<>();
    }

    @RabbitListener(queues = RabbitMQQueueConstant.DELAYED_QUEUE,concurrency = "1-2")
    public void delayedQueue(MyDateTest test){
        log.info("延时队列结束消息：{},接收时间：{},",test,new Date());
    }
}
